'use strict';

module.exports = function (grunt) {

    var configBuildPath = '.build/script/require.config.js';


    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        clean: {
            dist: {
                src: 'dist'
            },
            build: {
                src: ['.build', '.sass-cache']
            }
        },
        requirejs: {
            dist: {
                options: {
                    mainConfigFile: 'public/script/require.config.js',
                    baseUrl: "public/script",
                    dir: '.build/script',
                    optimize: 'uglify2',
                    modules: grunt.file.readJSON('tasks/module.json'),
                    preserveLicenseComments: false,
                    cjsTranslate: true,
                    onBuildWrite: function (moduleName, path, singleContents) {
                        if (moduleName.split('/')[0] === 'vendor') {
                            return;
                        }
                        return singleContents.replace(/define\s*\(/, 'define("' + moduleName + '",');
                    }
                }
            }
        },
        concatconfig: {
            options: {
                jsonPath: 'tasks/concat.json'
            },
            files: {
                expand: true,
                cwd: '.build/script',
                src: ['*', '!vendor'],
                filter: 'isDirectory'
            }
        },
        concat: {
            options: {
                separator: ';'
            },
            business: grunt.file.readJSON('tasks/concat.json')
        },
        modules: {
            options: {
                moduleJson: grunt.file.readJSON('tasks/module.json'),
                configBuildPath: configBuildPath
            },
            files: {
                expand: true,
                cwd: '.build/script',
                src: ['framework.js'],
                dest: 'dist/script'
            }
        },
        bundles: {
            options: {
                configBuildPath: configBuildPath
            },
            files: {
                expand: true,
                cwd: '.build/script',
                src: ['*', '!vendor'],
                dest: 'dist/script',
                filter: 'isDirectory'
            }
        },
        copy: {
            files: {
                expand: true,
                cwd: 'public',
                src: ['js/vendor/require/**/*.js', 'img/**', 'fonts/**'],
                dest: 'dist'
            }
        },
        hash: {
            dist: {
                options: {
                    assets: 'assets.json'
                },
                files: [
                    {
                        expand: true,
                        cwd: 'public/css',
                        src: ['module/**/*.css', 'animate/**/*.css'],
                        dest: 'dist/css'
                    },
                    {
                        expand: true,
                        cwd: '.build/script',
                        src: ['require.config.js'],
                        dest: 'dist/script'
                    }
                ]
            }
        },
        compass: {
            dist: {
                options: {
                    sassDir: 'public/prototype/sass',
                    cssDir: 'public/css',
                    outputStyle: 'compressed',
                    noLineComments: true
                }
            }
        },
        watch: {
            coffee: {
                files: ['public/coffee/**/*.coffee'],
                tasks: ['coffee']
            },
            sass: {
                files: ['public/prototype/**/*.scss'],
                tasks: ['compass']
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-requirejs');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-compass');
    grunt.loadTasks('tasks');

    grunt.registerTask('default', ['clean', 'requirejs', 'modules', 'concatconfig', 'concat', 'bundles', 'hash', 'copy', 'clean:build']);

};
